//
//  FSWPhotoPickManager.m
//  HMT-APP
//
//  Created by 伏圣文 on 2017/8/29.
//  Copyright © 2017年 LXC. All rights reserved.
//

#import "FSWPhotoPickManager.h"


@interface FSWPhotoPickManager()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

{
    UIImagePickerController *_imgPickC;
    UIViewController *_vc;
//    CallBackBlock _callBackBlock;
}
@property (strong, nonatomic) CallBackBlock callBackBlock;
@end

@implementation FSWPhotoPickManager

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static FSWPhotoPickManager *pickManager;
    dispatch_once(&onceToken, ^{
        pickManager = [[FSWPhotoPickManager alloc]init];
    });
    return pickManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        if (!_imgPickC) {
            _imgPickC = [[UIImagePickerController alloc]init]; // 初始化 _imagPickC
            _editPic = YES;
        }
    }
    return self;
}

- (void)presentPicker:(PickerType)pickerType target:(UIViewController *)vc callBackBlock:(CallBackBlock)callBackBlock
{
    _vc = vc;
    _callBackBlock = callBackBlock;
    if (pickerType == PickerType_Camera) {
        // 拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            _imgPickC.delegate = self;
            _imgPickC.sourceType = UIImagePickerControllerSourceTypeCamera;
            _imgPickC.allowsEditing = self.editPic;
            _imgPickC.showsCameraControls = YES;
            UIView *view = [[UIView alloc]init];
            view.backgroundColor = [UIColor grayColor];
            _imgPickC.cameraOverlayView = view;
            _imgPickC.modalPresentationStyle = UIModalPresentationFullScreen;
            [_vc presentViewController:_imgPickC animated:YES completion:nil];
        }else{
//            UIAlertView *alerV = [[UIAlertView alloc] initWithTitle:@"" message:@"相机不可用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [alerV show];
             
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];
              
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:cancleAction];
            [vc presentViewController:alertController animated:YES completion:nil];
            
        }
    }else if (pickerType == PickerType_Photo){
        // 相册
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            _imgPickC.delegate = self;
            _imgPickC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            _imgPickC.allowsEditing = self.editPic;
             _imgPickC.modalPresentationStyle = UIModalPresentationFullScreen;
            [_vc presentViewController:_imgPickC animated:YES completion:nil];
        }else{
//            UIAlertView *alerV = [[UIAlertView alloc] initWithTitle:@"" message:@"相册不可用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
//            [alerV show];
           UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"相册不可用" preferredStyle:UIAlertControllerStyleAlert];
                        
                      UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                      [alertController addAction:cancleAction];
                      [vc presentViewController:alertController animated:YES completion:nil];
        }
    }
    
    
   
    
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];//原图
    float w = image.size.width;
    float h = image.size.height;
    if (picker.allowsEditing) {
        if ((w < 320 && h < 480) || picker.allowsEditing == NO) {//如果图片  宽、高都分别小于320、480  那么，直接用截好的图,因为组件会自动把图片放大
            image = [info valueForKey:UIImagePickerControllerEditedImage];
        }
        else
        {//否则 就选择较短的(宽和高比较)作为标准 以中心为基准截取正方形
            image = [info valueForKey:UIImagePickerControllerEditedImage];
            //        w = image.size.width;
            //        h = image.size.height;
            //        float s = 0;
            //        s = MIN(w, h);
        }
    }

    @weakify(self);
    NSDictionary *dict = @{UIImagePickerControllerOriginalImage:image};
    
    _editPic = YES;
    [_vc dismissViewControllerAnimated:YES completion:^{
        @strongify(self);
        self.callBackBlock(dict, NO); // block回调
    }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    @weakify(self);
    [_vc dismissViewControllerAnimated:YES completion:^{
        @strongify(self);
        self.callBackBlock(nil, YES); // block回调
    }];
}


#pragma ------ Seter ------
- (void)setEditPic:(BOOL)editPic{
    _editPic = editPic;
    _imgPickC.allowsEditing = editPic;
}
@end
